package top.lyc829.mymusic.ui.fragment.index

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import top.lyc829.mymusic.base.BaseViewModel
import top.lyc829.mymusic.model.bean.*
import top.lyc829.mymusic.model.db.MyDataBase
import top.lyc829.mymusic.model.http.MyNetwork
import top.lyc829.mymusic.model.repository.IndexRepository

class IndexViewModel : BaseViewModel() {

    private var repository: IndexRepository =
        IndexRepository.getInstance(MyDataBase.instance.getBannerDao(), MyNetwork.getInstance())

    private val _bannerList: MutableLiveData<List<Banner>> by lazy { MutableLiveData() }
    val bannerList: LiveData<List<Banner>> get() = _bannerList

    private val _playList: MutableLiveData<List<PlayListSimple>> by lazy { MutableLiveData() }
    val playList: LiveData<List<PlayListSimple>> get() = _playList

    private val _topAlbums: MutableLiveData<List<WeekData>> by lazy { MutableLiveData() }
    val topAlbums: LiveData<List<WeekData>> get() = _topAlbums

    private val _newSongs: MutableLiveData<List<NewSongResultData>> by lazy { MutableLiveData() }
    val newSongs: LiveData<List<NewSongResultData>> get() = _newSongs


    val bottomType: MutableLiveData<TypeEnum> by lazy { MutableLiveData(TypeEnum.ALBUM) }

    fun getBanners() {
        launch(false,
            {
                val banners = repository.getBannerList()
                _bannerList.value = banners
            }, {
                errorLiveData.value = it
            })
    }

    fun getPlayList() {
        launch(false,
            {
                val playList = repository.getRecommendPlayList()
                _playList.value = playList
            }, {
                errorLiveData.value = it
            })
    }

    fun getTopAlbums() {
        launch(false,
            {
                _topAlbums.value = repository.getTopAlbums()
            }, {
                errorLiveData.value = it
            })
    }

    fun getNewSongs() {
        launch(false, {
            _newSongs.value = repository.getNewSongs()
        }, {
            errorLiveData.value = it
        })
    }
}